package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper,BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;



    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //  参数只有 category3Id！ 只能先获取base_category_trademark 表中的 trademark_id
        //  select * from base_category_trademark where category3_id = category3Id;
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);

        //  只需要品牌Id trademark_id
        //  映射处理：
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            List<Long> idList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                //  表示获取品牌Id;
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());

            //  根据这个idList 获取到 base_trademark;
            //  select * from base_trademark where id in (1,2,3,4,5);
            return this.baseTrademarkMapper.selectBatchIds(idList);
        }
        //  默认返回
        return null;
    }

    @Override
    public void removeByCategory3IdAndtradeMarkId(Long category3Id, Long trademarkId) {
        //  本质执行更新数据 ：逻辑删除！
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id",category3Id);
        baseCategoryTrademarkQueryWrapper.eq("trademark_id",trademarkId);
        this.baseCategoryTrademarkMapper.delete(baseCategoryTrademarkQueryWrapper);

    }

    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
        //  {category3Id: 61, trademarkIdList: [5, 6]}
        //  插入数据： base_category_trademark
        //  遍历数组    第一种：
        //        categoryTrademarkVo.getTrademarkIdList().forEach(trademarkId -> {
        //            //  创建对象
        //            BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
        //            baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
        //            baseCategoryTrademark.setTrademarkId(trademarkId);
        //            baseCategoryTrademarkMapper.insert(baseCategoryTrademark);
        //        });

        //  映射关系！   第二种：
        List<BaseCategoryTrademark> baseCategoryTrademarkList = categoryTrademarkVo.getTrademarkIdList().stream().map(trademarkId -> {
            BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
            //  赋值三级分类Id
            baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
            //  赋值品牌Id
            baseCategoryTrademark.setTrademarkId(trademarkId);
            //  返回一个对象
            return baseCategoryTrademark;
        }).collect(Collectors.toList());

        //  批量插入数据！
        this.saveBatch(baseCategoryTrademarkList);

    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        //  两个集合获取差集
        //  select * from base_category_trademark where category3_id = category3Id;
        /*
         3, 华为, http://47.93.148.192:8080/group1/M00/01/71/rBHu8mEQpUuAVioLAAGXnmYhX7M923.jpg, 2021-08-13 15:18:41.0, 2021-08-13 15:18:41.0, 0
         2, 苹果, http://47.93.148.192:8080/group1/M00/01/71/rBHu8mEQpOuAXIroAAA8KOpezoQ651.png, 2021-08-13 15:18:41.0, 2021-08-13 15:18:41.0, 0
         1, 小米, http://47.93.148.192:8080/group1/M00/01/71/rBHu8mEQpLiAP5NOAAAFtscZn_s397.png, 2021-08-13 15:18:41.0, 2021-08-13 15:18:41.0, 0
         */
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id",category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);


        //  第一个集合！获取已经绑定好的品牌！
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)){
            //  根据category3_id 获取到品牌Id trademark_id
            //  获取品牌所对应的Id  1,2,3
            List<Long> idList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                //  表示获取品牌Id;
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            //  获取所有：select * from base_trademark; 1,2,3,5,6
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectList(null).stream().filter(baseTrademark -> {
                //  {1,2,3}  {1,2,3,5,6}  返回{5,6}
                return !idList.contains(baseTrademark.getId());
            }).collect(Collectors.toList());

            return baseTrademarkList;
        }

        //  没有绑定查询所有品牌集合数据！
        return baseTrademarkMapper.selectList(null);
    }
}
